/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Config.Config;
import Util.ActiveDirectory;
import java.util.ArrayList;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;

/**
 *
 * @author ddavilac
 */
public class User extends Persona {
    
    private static User usuario;
    
    private User() throws Exception{        
        //Conecta al DA
        ActiveDirectory ad = new ActiveDirectory(
                                    Config.getProperty("DCLoginName"), 
                                    Config.getProperty("DCLoginPss"), 
                                    Config.getProperty("DCName"), 
                                    Config.getProperty("DCPort"));
        //Busca al usuario logueado en Windows
        ArrayList tempArrayList = ad.searchUser(System.getProperty("user.name"));                

        //Carga el usuario en memoria
        for(int i =0; i < tempArrayList.size(); i++){
            
            Attributes attrs = ((SearchResult)tempArrayList.get(i)).getAttributes();                               
            usuario.setNombre(attrs.get("displayName").toString());
            usuario.setCargo(attrs.get("title").toString());
            usuario.setEmpresa(attrs.get("company").toString());
            usuario.setDependencia(attrs.get("department").toString());
            usuario.setDireccion(attrs.get("streetAddress").toString()+" "+attrs.get("street").toString());
            usuario.setTelefono(attrs.get("telephoneNumber").toString());
            usuario.seteMail(attrs.get("mail").toString());  
        } 
    }
    
    public static User getInstance() throws Exception{   
        if (usuario == null) {            
            return new User();
        }                            
        return usuario;
    }
}
